package com.sdnu.service.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdnu.common.exceptionhandle.CustomerException;
import com.sdnu.common.utils.CreateNamePicture;
import com.sdnu.common.utils.MD5;
import com.sdnu.service.entity.ucenter.User;
import com.sdnu.service.entity.ucenter.excel.UserDemo;
import com.sdnu.service.entity.ucenter.vo.LoginVo;
import com.sdnu.service.mapper.UserMapper;
import com.sdnu.service.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdnu.service.service.oss.OssService;
import com.sdnu.service.utiils.excel.WebExcelListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Zhao YunLai
 * @since 2021-12-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private OssService ossService;

    @Override
    public void addUserBatch(MultipartFile file, UserService userService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, UserDemo.class,new WebExcelListener(userService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean addOneUser(UserDemo userDemo) {
        User user = new User();
        System.out.println(userDemo);
        BeanUtils.copyProperties(userDemo,user);
        String account = userDemo.getAccount();
        String password = account.substring(account.length() - 4);
        password= MD5.encrypt(password);
        user.setPassword(password);

        //这里根据用户姓名生成用户头像，并把头像上传
        //暂时使用本地路径作为中转，之后可以删除
        String filePath = "E:\\code_workspace00\\utils_parent\\static\\avatar";
        //生成图片保存到本地文件夹并且上传到阿里云oss中返回图片地址
        String avatarUrl =createAndUploadUserAvatar(userDemo.getName(), filePath);
        //设置头像
        user.setAvatar(avatarUrl);

        Integer gender = userDemo.getSex().equals("男")?0:1;
        user.setGender(gender)
                .setIntro("你好，我是"+userDemo.getName());

        int insert = baseMapper.insert(user);
        return insert==1;
    }

    @Override
    public User login(LoginVo loginVo) {
        //表单信息由前端验证
        String account = loginVo.getAccount();
        String password = loginVo.getPassword();

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account",account);
        User user = baseMapper.selectOne(wrapper);
        if(user==null){
            throw new CustomerException(20001,"用户不存在！");
        }
        if(!user.getPassword().equals(MD5.encrypt(password))){
            throw new CustomerException(20001,"密码错误！");
        }
        //日志
        System.out.println(new Date()+":"+user.getName()+"-->登录");
        return user;
    }

    @Override
    public Boolean changePassword(LoginVo loginVo) {
        //表单前端验证完毕，关键数值非空
        User user = this.getById(loginVo.getUserId());
        if(user==null){
            throw new CustomerException(20001,"当前用户不存在！");
        }
        if(!user.getPassword().equals(MD5.encrypt(loginVo.getPassword()))){
            throw new CustomerException(20001,"旧密码错误！");
        }
        user.setPassword(MD5.encrypt(loginVo.getNewPassword()));
        baseMapper.updateById(user);
        //日志
        System.out.println(new Date()+":"+user.getName()+"-->修改密码");
        return true;
    }

    @Override
    public String createAndUploadUserAvatar(String userName,String filePath) {

        try {
            CreateNamePicture.generateImg(userName,filePath,userName);
            filePath=filePath+"/"+userName+".jpg";

            return ossService.uploadFileByLocalPath(filePath, "avatar");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
